<?php 
	include "../service/util/authenticateSession.php";
	include ("messages.properties.php"); 
	$url = ($_SERVER['REQUEST_URI']);
	if (!preg_match('/^(id=[1-4])|(id=[1-4]&msg=[ok|no|upok|upno])*$/', substr($url, strrpos($url, "?") + 1, strlen($url))))
		echo "<script>window.location.href= 'index.php';</script>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Chi Tiết Dịch Vụ</title>
	<?php 
		include("import.php");
		include_once("../service/serviceService.php");
	?>
    <script type="text/javascript">
		$(document).ready(function() {
			$('label.en').click(function() {
                    $('#en').toggle("normal");
			});
		
			$('label.vn').click(function() {
				$('#vn').toggle("normal");
			});
		
			$('#en').toggle("normal");
		});
		function validate(){
			return $("#img").val() != "";
		}
    </script>
</head>
<body>
<?php include "header.php"; ?>
<form id="form" enctype="multipart/form-data">
<!-- Container -->
<div id="container">
	<div class="shell">
		<div id="main">
			<div class="cl">&nbsp;</div>
			<!-- Content -->
			<div id="content">        
            	<?php
					$message = "";
					$class = "";
					if (isset($_GET['msg'])) $msg = $_GET['msg'];
					
					if(isset($msg)){
						if($msg == "upok") {
							$message = $messages["uploadImageServiceOk"];
							$class = "msg msg-ok";
						}else if($msg == "upno"){
							$message = $messages["uploadImageServiceFailed"];
							$class = "msg msg-error"; 
						}else if($msg == "ok"){
							$message = $messages["updateServiceOk"];
							$class = "msg msg-ok"; 
						}else if($msg == "no"){
							$message = $messages["updateServiceFailed"];
							$class = "msg msg-error"; 
						}
					}
					if ($message == "") $class = 'msg msg-hidden';
				?>
				<div class="<?php echo $class; ?>" id="msg">
					<p><strong><?php echo $message; ?></strong></p>
					<a href="#" onclick="hideMessage();" class="close">close</a>
				</div>
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Thông Tin Dịch Vụ</h2>
					</div>
					<!-- End Box Head -->
					<!-- Form -->
                    <div class="form">
                        <?php
                            $imagePath = '../images/4s/'.$_GET["id"]."/";
                            $directory = opendir($imagePath);
                            $images = array();
                            $count = 0;
                            
                            while (($file = readdir($directory)) != false) {
                                if ($file != "." && $file != ".." && $file != ".svn") {
                                    $images[substr($file, 0, 1)] = $file;
                                    $count = substr($file, 0, 1);
                                }
                            }
                            closedir($directory);
							
							$result = findById($_GET["id"]);
							$row = ConnectionManager::getInstance()->fetch($result);
                        ?>
						<label class="vn">Tựa đề Việt</label>
						<div id="vn">
							<p id="title-vn">
								<input name="titleVn" type="text" class="field size1" value="<?php echo $row["SEV_TITLE_VN"]; ?>"/>
							</p>
							<p id="content-vn">
								<label>Nội dung Việt</label>
								<textarea id="contentVn" name="contentVn" class="ckeditor"><?php echo $row["SEV_CONTENT_VN"]; ?></textarea>
							</p>
						</div>
						
						<label class="en">Tựa đề Anh</label>
						<div id="en">
							<p id="title-en">
								<input name="titleEn" type="text" class="field size1" value="<?php echo $row["SEV_TITLE_EN"]; ?>"/>
							</p>
							<p id="content-en">
								<label>Nội dung Anh</label>
								<textarea id="contentEn" name="contentEn" class="ckeditor"><?php echo $row["SEV_CONTENT_EN"]; ?></textarea>
							</p>
						</div>
                        <p>
                            <label>Hình ảnh</label>
                            <input type="file" class="field size1" id="img" name="img"/>
                            <input type="button" value="Upload" onclick="if(validate()) submitForm('form', '../action/uploadImageServiceAction.php?serviceId=<?php echo $_GET["id"]; ?>','post');">
                            <input type="hidden" value="<?php echo $count + 1; ?>" name="nextId">
                        </p>
                        <?php
                            while($each = current($images)){	
                        ?>
                                <p><img src="<?php echo $imagePath.$each;?>" height="30%" width="30%" align="top" />
                                <a href="../action/deleteImageServiceAction.php?path=<?php echo $imagePath.$each;?>&id=<?php echo $_GET["id"];?>">X</a></p>
                        <?php
                                next($images);
                            }
                        ?>	
                    </div>
                    <!-- End Form -->
                    <!-- Form Buttons -->
                    <div class="buttons">
                        <input type="button" class="button" value="Chấp Nhận" onclick="submitForm('form', '../action/updateServiceAction.php?id=<?php echo $_GET["id"]; ?>', 'post');" />
                        <input type="button" class="button" value="Trở Về" onclick="window.location = 'user.php';" />
                    </div>
                    <!-- End Form Buttons -->
				</div>
				<!-- End Box -->
			</div>
			<!-- End Content -->
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->
</form>
<?php
	include "footer.php";
	ConnectionManager::getInstance()->closeConnection();
?>
</body>
</html>
